#! /usr/bin/env python
# -*- coding: utf-8 -*-
import time

from playwright.sync_api import Page

from common.base_page import BasePage
from common.locate_by import By
from common.logger import logger
from locators.majorHazardSourceSafety.olMonitorWarn.videoSurveillance.loc_query import LocQuery


class VideoSurveillance(BasePage):

    def __init__(self, page: Page):
        super().__init__(page)
        self.switch_menu('重大危险源安全管理')
        self.switch_menu('在线监测预警')
        self.switch_menu('视频监控')
        time.sleep(1)

    def query_video(self, video_type, video_name):
        logger.info('开始视频监控查询测试')
        logger.info('选择视频列表tab页')
        self.click(LocQuery.loc_list_tab)
        logger.info('选择视频类型')
        self.select(LocQuery.loc_video_type, video_type)
        logger.info('输入监控名称')
        self.fill(LocQuery.loc_name_input, video_name)
        logger.info('点击查询按钮')
        self.click(LocQuery.loc_query_button)

    def favor_video(self, video_name):
        logger.info('开始监控视频收藏测试')
        num = self.page.get_by_role(By.treeitem, name=video_name).get_by_role(By.img).count()
        # 有报警的摄像头会有三个图标，第一个是报警图标，第二个是收藏，第三个是播放
        if num == 3:
            self.page.get_by_role(By.treeitem, name=video_name).get_by_role(By.img).nth(1).click()
        else:
            self.page.get_by_role(By.treeitem, name=video_name).get_by_role(By.img).nth(0).click()

    def play_video(self, video_name):
        logger.info('开始监控视频播放测试')
        num = self.page.get_by_role(By.treeitem, name=video_name).get_by_role(By.img).count()
        # 有报警的摄像头会有三个图标，第一个是报警图标，第二个是收藏，第三个是播放
        if num == 3:
            self.page.get_by_role(By.treeitem, name=video_name).get_by_role(By.img).nth(2).click()
        else:
            self.page.get_by_role(By.treeitem, name=video_name).get_by_role(By.img).nth(1).click()
